"""
This module defines functions for customizing the plugins used at Kolibri build time
the default plugins used at Kolibri run time, and the default value for
the DJANGO_SETTINGS_MODULE environment variable.

For more detail see the documentation in __init__.py
"""
import logging
import os
import sys
import tempfile

import requests

sys.path.append(os.path.realpath(os.path.join(os.path.dirname(__file__), "..")))

logger = logging.getLogger(__name__)
plugins_cache = {}


def load_plugins_from_file(file_path):
    global plugins_cache
    if file_path not in plugins_cache:
        # We have been passed a URL, not a local file path
        if file_path.startswith("http"):
            logger.info(
                "Downloading plugins manifest from {file_path}".format(
                    file_path=file_path
                )
            )
            _, path = tempfile.mkstemp(suffix=".txt", text=True)
            with open(path, "w") as f:
                r = requests.get(file_path)
                f.write(r.content)
            file_path = path
        with open(file_path, "r") as f:
            plugins_cache[file_path] = [
                plugin.strip() for plugin in f.readlines() if plugin.strip()
            ]
    return plugins_cache[file_path]


build_config_path = os.path.join(
    os.path.dirname(__file__), "../kolibri/utils/build_config"
)

default_settings_template = "settings_path = '{path}'"


def set_default_settings_module():
    if (
        "DEFAULT_SETTINGS_MODULE" in os.environ
        and os.environ["DEFAULT_SETTINGS_MODULE"]
    ):
        default_settings_path = os.environ["DEFAULT_SETTINGS_MODULE"]
        with open(os.path.join(build_config_path, "default_settings.py"), "w") as f:
            # Just write out settings_path = '<settings_path>'
            logger.info(
                "Setting default settings module to {path}".format(
                    path=default_settings_path
                )
            )
            f.write(default_settings_template.format(path=default_settings_path))


run_time_plugin_template = "DEFAULT_PLUGINS = {plugins}\n"


def set_run_time_plugins():
    if "RUN_TIME_PLUGINS" in os.environ and os.environ["RUN_TIME_PLUGINS"]:
        runtime_plugins = load_plugins_from_file(os.environ["RUN_TIME_PLUGINS"])
        with open(os.path.join(build_config_path, "default_plugins.py"), "w") as f:
            # Just write out 'plugins = [...]' <-- list of plugins
            logger.info("Setting run time plugins to:")
            for runtime_plugin in runtime_plugins:
                logger.info(runtime_plugin)
            logger.info("### End run time plugins ###")
            f.write(run_time_plugin_template.format(plugins=runtime_plugins.__str__()))


if __name__ == "__main__":
    set_default_settings_module()
    set_run_time_plugins()
